// Copyright (c) 2008 Matthew Johnson
// Licensed under the MIT License (see license.txt)
package actionml.display {
	import actionml.Global;
	import actionml.animation.Tween;

	import flash.display.Sprite;
	import flash.utils.getTimer;	
	
	public class PlayTween {
		public static function initialize() {
			Global.initializeExtension(PlayTween, Sprite);
		}
		public static var instanceMethods:Object = {
			playTween: function(t:*) {
				if (t is Tween) {
					t.play();
					return t;
				} else if (t is String) {
					if ("tweens" in this && t in this.tweens) {	
						this.tweens[t].play();
						return this.tweens[t];
					} else if (Global["tweens"] && t in Global["tweens"]) {
						var tweenProperties:Object = Global["tweens"][t].copyProperties();
						tweenProperties.target = this;
						this.tweens ||= [];
						this.tweens[t] = new Tween(tweenProperties);
						this.tweens[t].play();
						return this.tweens[t];
					} else {
						PlayTween["getTracer"]({method:"playTween", context:"arg"}).error("'" + t + "' was provided to playTween but is not the name of a tween that could be found.");
						return null;
					}
				} else {
					t.target = this;
					var tween:Tween = new Tween(t);
					tween.play();
					return tween;
				}
			},
			cancelDelayedTweens: function() {
				this.delayedTweensCancelled = getTimer();
			}
		}
	}
}